<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="./2048.css">
    <title>Document</title>
</head>
<body>
<div class="content">
    <div id="content">

    </div>

</div>
<script>
    const lines = 4
    let data = []
    emptyPosition = []
    generateData()
    generateNewNumber()
    generateNewNumber()
    generateTable()

    // 初始化数据
    function generateData() {
        data = []
        let arr
        for (let i = 0; i < lines; i++) {
            arr = []
            for (let j = 0; j < lines; j++) {
                arr.push(0)
            }
            data.push(arr)
        }
        generateEmptyPosition()
    }

    // 数据生成表格
    function generateTable() {
        let html = ''
        for (let i in data) {
            const width = lines * 153
            let line = '<div style="width:' + width + 'px"  class="line">'
            for (let j in data[i]) {
                line += '<div class="item '
                const num = data[i][j]
                switch (num) {
                    case 0:
                        line += '"> '
                        break
                    case 2:
                        line += 'n2"> ' + num
                        break
                    case 4:
                        line += 'n4"> ' + num
                        break
                    case 8:
                        line += 'n8"> ' + num
                        break
                    case 16:
                        line += 'n16"> ' + num
                        break
                    case 32:
                        line += 'n32"> ' + num
                        break
                    case 64:
                        line += 'n64"> ' + num
                        break
                    case 128:
                        line += 'n128"> ' + num
                        break
                    case 256:
                        line += 'n256"> ' + num
                        break
                    case 512:
                        line += 'n512"> ' + num
                        break
                    case 1024:
                        line += 'n1024"> ' + num
                        break
                    case 2048:
                        line += 'n2048"> ' + num
                        break
                }
                line += '</div>'
            }
            html += line + '</div>'
        }
        console.log(html)
        document.getElementById('content').innerHTML = html
    }

    document.onkeydown = function (e) {
        let key = window.event.keyCode
        switch (key) {
            case 37:
                actionLeft()
                e.preventDefault()
                break
            case 38:
                actionUp()
                e.preventDefault()
                break
            case 39:
                actionRight()
                e.preventDefault()
                break
            case 40:
                actionDown()
                e.preventDefault()
                break
        }
        if (emptyPosition.length === 0) {

        } else {
            generateNewNumber()
        }
    }

    // 生成随机数
    function generateNewNumber() {
        let position = Math.floor(Math.random() * emptyPosition.length)
        let num = Math.round(Math.random()) === 0 ? 2 : 4
        assignment(position, num)
        generateEmptyPosition()
    }

    /*
    *  position 为在 emptyPosition 集合里的下标
    *  num 为要填入的数
    * */
    function assignment(position, num) {
        let dataPosition = emptyPosition[position]
        let dataLine = parseInt(String(dataPosition / lines))
        let linePosition = Math.round(dataPosition % lines)
        data[dataLine][linePosition] = num
        generateTable()
    }

    function actionLeft() {
        for (let i in data) {
            let temp = 0
            let index = 0
            for (let j in data[i]) {
                //当前位置不为 0
                if (data[i][j]) {
                    // 临时值为0  赋值给临时值
                    if (temp === 0) {
                        temp = data[i][j]
                        data[i][j] = 0
                    } else if (temp === data[i][j]) { //临时值不为0 判断是否和当前值相同 相同相加并赋值给index位
                        data[i][index] = temp * 2
                        data[i][j] = 0
                        temp = 0
                        index++
                    } else { //临时值不为0 且和当前值不同 临时值赋值给 index 当前值赋值给临时值
                        data[i][index] = temp
                        temp = data[i][j]
                        data[i][j] = 0
                        index++
                    }
                }
            }
            if (temp) {
                data[i][index] = temp
            }
        }
        generateEmptyPosition()
    }

    function actionUp() {
        for (let i in data) {
            let temp = 0
            let index = 0
            for (let j in data) {
                if (data[j][i]) {
                    if (temp === 0) {
                        temp = data[j][i]
                        data[j][i] = 0
                    } else if (temp === data[j][i]) {
                        data[index][i] = temp * 2
                        data[j][i] = 0
                        temp = 0
                        index++
                    } else {
                        data[index][i] = temp
                        temp = data[j][i]
                        data[j][i] = 0
                        index++
                    }
                }
            }
            if (temp) {
                data[index][i] = temp
            }
        }
        generateEmptyPosition()
    }

    function actionRight() {
        for (let i in data) {
            let temp = 0
            let index = lines - 1
            for (let j = lines - 1; j >= 0; j--) {
                //当前位置不为 0
                if (data[i][j]) {
                    if (temp === 0) {
                        temp = data[i][j]
                        data[i][j] = 0
                    } else if (temp === data[i][j]) {
                        data[i][index] = temp * 2
                        temp = 0
                        data[i][j] = 0
                        index--
                    } else {
                        data[i][index] = temp
                        temp = data[i][j]
                        data[i][j] = 0
                        index--
                    }
                }
            }
            if (temp) {
                data[i][index] = temp
            }
        }
        generateEmptyPosition()
    }

    function actionDown() {
        for (let i in data) {
            let temp = 0
            let index = lines - 1
            for (let j = lines - 1; j >= 0; j--) {
                if (data[j][i]) {
                    if (temp === 0) {
                        temp = data[j][i]
                        data[j][i] = 0
                    } else if (temp === data[j][i]) {
                        data[index][i] = temp * 2
                        data[j][i] = 0
                        temp = 0
                        index--
                    } else {
                        data[index][i] = temp
                        temp = data[j][i]
                        data[j][i] = 0
                        index--
                    }
                }
            }
            if (temp) {
                data[index][i] = temp
            }
        }
        generateEmptyPosition()
    }

    function generateEmptyPosition() {
        emptyPosition = []
        data.forEach((i, iIndex) => {
            i.forEach((j, jIndex) => {
                if (!j) {
                    emptyPosition.push(iIndex * lines + jIndex)
                }
            })
        })
    }
</script>
</body>
</html>
